package com.hua.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hua.error.CustomException;
import com.hua.pojo.Comment;
import com.hua.result.R;
import com.hua.service.CommentService;
import com.hua.utils.HuaUtil;
import com.hua.vo.WriteCommentData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 获取评论
 * @author ONE-X
 * @date 2020/11/12
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
	@Autowired
	CommentService commentService;
	/**
	 * 获取评论
	 */
	@ResponseBody
	@PostMapping("/info/{flag}/{tid}")
	public String getCommentInfo(@PathVariable String flag,@PathVariable String tid){
		if(!("1".equals(flag) || "2".equals(flag))){
			throw new CustomException("没有该主题");
		}
		// 根据  flag 去判断该获取 博客/问题 的评论
		QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
		commentQueryWrapper.eq("topic_category",flag);
		commentQueryWrapper.eq("topic_id",tid);
		commentQueryWrapper.orderByDesc("gmt_create");
		List<Comment> list = commentService.list(commentQueryWrapper);

		return JSON.toJSONString(list);
	}

	/**
	 * 写博客
	 * @return
	 */
	@ResponseBody
	@PostMapping("/write")
	public String writeComment(@RequestBody WriteCommentData writeCommentData){
		commentService.addComment(writeCommentData);
		return JSON.toJSONString(R.ok());
	}

	/**
	 * 获取用户的问答评论
	 * @return
	 */
	@ResponseBody
	@PostMapping("/userInfo")
	public String getUserInfo(){
		// 获取当前用户
		String uid = HuaUtil.returnAuthentication();
		List<Comment> userComment = commentService.findCommentQuestion(uid);
		return JSON.toJSONString(userComment);
	}	/**
	 * 获取用户的问答评论
	 * @return
	 */
	@ResponseBody
	@PostMapping("/blogInfo")
	public String getBlogInfo(){
		// 获取当前用户
		String uid = HuaUtil.returnAuthentication();
		List<Comment> userComment = commentService.findCommentBlog(uid);
		return JSON.toJSONString(userComment);
	}
	/**
	 * 删除该用户的评论
	 */
	@ResponseBody
	@PostMapping("/delComment/{cid}")
	public String delUserComment(@PathVariable String cid){
		commentService.remove(new QueryWrapper<Comment>().eq("comment_id",cid));
		return JSON.toJSONString(R.ok());
	}

}
